<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
	</head>
	<body>
	
	</body>
</html>
<script>
	function Node(data, left, right) {
		this.data = data;
		this.left = left;
		this.right = right;
	}
	Node.prototype = {
		show: function() {
			console.log('this.data', this.data)
			console.log('this.left', this.left)
			console.log('this.right', this.right)
		}
	}
	var a = new Node(1, 2, 3)
	a.show()
	console.log('Node', Node)

	function Tree() {
		this.root = null;
	}

	Tree.prototype = {
		insert: function(data) {
			var node = new Node(data, null, null);
			if (!this.root) {
				this.root = node;
				return;
			}
			var current = this.root;
			var parent = null;
			while (current) {
				parent = current;
				if (data < parent.data) {
					current = current.left;
					if (!current) {
						parent.left = node;
						return;
					}
				} else {
					current = current.right;
					if (!current) {
						parent.right = node;
						return;
					}
				}

			}
		},
		preOrder: function(node) {
			if (node) {
				node.show();
				this.preOrder(node.left);
				this.preOrder(node.right);
			}
		},
		middleOrder: function(node) {
			if (node) {
				this.middleOrder(node.left);
				node.show();
				this.middleOrder(node.right);
			}
		},
		laterOrder: function(node) {
			if (node) {
				this.laterOrder(node.left);
				this.laterOrder(node.right);
				node.show();
			}
		},
		getMin: function() {
			var current = this.root;
			while (current) {
				if (!current.left) {
					return current;
				}
				current = current.left;
			}
		},
		getMax: function() {
			var current = this.root;
			while (current) {
				if (!current.right) {
					return current;
				}
				current = current.right;
			}
		},
		getDeep: function(node, deep) {
			deep = deep || 0;
			if (node == null) {
				return deep;
			}
			deep++;
			var dleft = this.getDeep(node.left, deep);
			var dright = this.getDeep(node.right, deep);
			return Math.max(dleft, dright);
		},
		getNode: function(data, node) {
			if (node) {
				if (data === node.data) {
					return node;
				} else if (data < node.data) {
					return this.getNode(data, node.left);
				} else {
					return this.getNode(data, node.right);
				}
			} else {
				return null;
			}
		}
	}
	var t = new Tree();
	t.insert(3);
	t.insert(8);
	t.insert(1);
	t.insert(2);
	t.insert(5);
	t.insert(7);
	t.insert(6);
	t.insert(0);
	console.log('t', t);
	// t.middleOrder(t.root);
	console.log(t.getMin(), t.getMax());
	console.log(t.getDeep(t.root, 0));
	console.log(t.getNode(5,t.root));
	-----------------------------------------------基础部分--------------------------------------
	var a = [1,10,3,4,5,6,7,8,9,0]
	var b=0;
	
	function mathMax(arrs) {
	  var max = arrs[0];
	  for(var i = 1,ilen = arrs.length; i < ilen; i++) {
	    if(arrs[i] > max) {
	      max = arrs[i];
	    }
	  }
	  return max;
	}
	a.map((item,index)=>{
		if(a[index] > b ){
			b=a[index]
		}
	})
	
	console.log('mathMax-----',mathMax(a))
	
	
	console.log('b==1',b)
	console.log('A-max',a.sort(function(a,b){return b-a })[0])
	
	
	
</script>
